﻿//-----------------------------------------------------------------------
// <copyright file="JsonpModule.cs" company="CMSN Software">
//    Copyright © 2010  CMSN Software
//    This program is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    (at your option) any later version.
//
//    This program is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with this program.  If not, see http://www.gnu.org/licenses.
// </copyright>
//-----------------------------------------------------------------------
namespace CMSNSoftwareTutorials.HowToUseCrossDomainAjaxRequest.CrossDomainWebService
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;

    /// <summary>
    /// Custom Http module for support Jsonp response
    /// </summary>
    public class JsonpModule : IHttpModule
    {
        /// <summary>
        /// callback Parameter Name for Jsonp request
        /// </summary>
        private const string CallbackParameterName = "callback";

        /// <summary>
        /// Initializes a module and prepares it to handle requests.
        /// </summary>
        /// <param name="context">An <see cref="T:System.Web.HttpApplication"/> that provides access to the methods, properties, and events common to all application objects within an ASP.NET application</param>
        public void Init(HttpApplication context)
        {
            context.BeginRequest += new EventHandler(this.Application_BeginRequest);
            context.EndRequest += new EventHandler(this.Application_EndRequest);
        }

        /// <summary>
        /// Disposes of the resources (other than memory) used by the module that implements <see cref="T:System.Web.IHttpModule"/>.
        /// </summary>
        public void Dispose()
        {
        }

        /// <summary>
        /// Handles the BeginRequest event of the Application control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void Application_BeginRequest(object sender, EventArgs e)
        {
            HttpApplication httpApplication = (HttpApplication)sender;

            ////Make sure only apply to Jsonp requests
            if (!string.IsNullOrEmpty(httpApplication.Context.Request.Params[CallbackParameterName]))
            {
                if (string.IsNullOrEmpty(httpApplication.Context.Request.ContentType))
                {
                    httpApplication.Context.Request.ContentType = "application/json; charset=utf-8";
                }

                httpApplication.Context.Response.Write(httpApplication.Context.Request.Params[CallbackParameterName] + "(");
            }
        }

        /// <summary>
        /// Handles the EndRequest event of the Application control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void Application_EndRequest(object sender, EventArgs e)
        {
            HttpApplication httpApplication = (HttpApplication)sender;
            if (!string.IsNullOrEmpty(httpApplication.Context.Request.Params[CallbackParameterName]))
            {
                httpApplication.Context.Response.Write(")");
            }
        }
    }
}